/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.account;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import apus.action.Action;
import apus.action.ActionResult;
import apus.authorisation.UserAuthorisation;
import apus.entity.Account;
import apus.entity.User;
import apus.persistence.AccountDAO;
import apus.persistence.OrganizationDAO;
import apus.persistence.PersonDAO;
import apus.persistence.UserDAO;
import apus.services.ServiceLocator;

/**
 *
 * @author Admin
 */
public class AccountListAction implements Action {

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        List<Account> accountList;
        AccountDAO accountDAO = ServiceLocator.getService(AccountDAO.class);
        String userLogin = ((UserAuthorisation) request.getSession().getAttribute("userAuth")).getLogin();
        UserDAO udao = ServiceLocator.getService(UserDAO.class);
        User user = udao.findByLogin(userLogin);

        if (request.getParameter("sub_id") != null) {
            int sub_id = Integer.parseInt(request.getParameter("sub_id"));

            if (((UserAuthorisation) request.getSession().getAttribute("userAuth")).getRole() == UserAuthorisation.Role.USER) {
                int sub_user_id = user.getSubscriber().getId();
                if (sub_id != sub_user_id) {
                    if (user.getSubscriber().getSubscriberType().equals("person")) {
                        request.setAttribute("person", ServiceLocator.getService(PersonDAO.class).read(sub_user_id));
                    } else {
                        request.setAttribute("organization", ServiceLocator.getService(OrganizationDAO.class).read(sub_user_id));
                    }
                    return ActionResult.redirect("main.view");
                }
            }

            accountList = accountDAO.readBySubscriber(sub_id);
        } else {

            if (((UserAuthorisation) request.getSession().getAttribute("userAuth")).getRole() == UserAuthorisation.Role.USER) {
                int sub_user_id = user.getSubscriber().getId();
                if (user.getSubscriber().getSubscriberType().equals("person")) {
                    request.setAttribute("person", ServiceLocator.getService(PersonDAO.class).read(sub_user_id));
                } else {
                    request.setAttribute("organization", ServiceLocator.getService(OrganizationDAO.class).read(sub_user_id));
                }
                return ActionResult.redirect("main.view");
            }

            accountList = accountDAO.findAll();
        }
        request.setAttribute("accountList", accountList);
        return ActionResult.forward("/WEB-INF/View/AccountView/AccountListView.jsp");
    }

    @Override
    public String getName() {
        return "account.list";
    }
}